package com.popo.android.activity;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.BounceInterpolator;
import android.widget.Button;

import com.popo.android.R;
import com.popo.android.service.LocalBoundService;
import com.popo.android.service.LocalBoundService.MyIBinder;
import com.popo.android.service.MessagerService;

public class LocalServiceActivity extends Activity implements OnClickListener {
    
    private ServiceConnection conn = new ServiceConnection() {
        
        Service s=null;
        
        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
//            s.stopSelf();
            s=null;
        }
        
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            
            s=((MyIBinder)service).getService();
            
        }
    };
    
    boolean bind =false;
    
    boolean mbind=false;
    Messenger messenger =null;
    protected ServiceConnection messagerConn = new ServiceConnection() {
        
        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
            messenger=null;
            System.out.println("messeger onServiceDisconnected");
          
        }
        
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            messenger= new Messenger(service);
            System.out.println("messeger connected");
          
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.localserviceactivity);
        Button btn1 = (Button) findViewById(R.id.button1 );
        Button btn2 = (Button) findViewById(R.id.button2 );
        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(LocalServiceActivity.this, LocalBoundService.class);
                bindService(i, conn, BIND_AUTO_CREATE);
                bind=true;
            }
        });
        
        btn2.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(bind){
                    unbindService(conn);
                    bind=false;
                }
            }
        });
        
        Button btn3 = (Button) findViewById(R.id.button3);
        Button btn4 = (Button) findViewById(R.id.button4);
        Button btn5 = (Button) findViewById(R.id.button5);
        btn3.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                
                Intent i  = new Intent(LocalServiceActivity.this, MessagerService.class);
                bindService(i, messagerConn , 0 );
            }
        });
        
        btn4.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                unbindService(messagerConn);
            }
        });
        
        btn5.setOnClickListener(this);
        
        
        
        
    }
    
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.button5:
                sayHello();
                break;

            default:
                break;
        } 
    }

    private void sayHello() {
        // TODO Auto-generated method stub
        if(messenger!=null)
        {
            Message m = new Message();
            m.what=MessagerService.SAY_HELLO;
            try {
                messenger.send(m);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}   
